/**
 * Copyright (C) 2010 Sergey Grigoriev <s.a.grigoriev@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.gmf.core

import org.apache.maven.project.MavenProject
import org.apache.commons.io.FilenameUtils
import org.apache.maven.model.Dependency
import org.apache.maven.plugin.MojoExecutionException
import org.apache.maven.artifact.Artifact
import java.util.regex.Matcher

/**
 * Defines flex project structure
 */
class FlexProjectUtils {

  static String TEST_RUNNER_SWF = "TestRunner.swf"

  private MavenProject mavenProject;

  def FlexProjectUtils(mavenProject) {
    this.mavenProject = mavenProject;
  }

  /**
   * The path  of compilation target file
   * @return the file which is result of compilation process
   */
  final File compilerOutput = new File([
          mavenProject.build.directory,
          "${mavenProject.build.finalName}.${mavenProject.artifact.getType()}"
  ].join(File.separator))

  final String getTestCompilationTarget() {
    [mavenProject.build.testOutputDirectory, TEST_RUNNER_SWF].join(File.separator)
  }

  def static parseArtifactString(String artifactString) {

    def p = ~/([^\:]+)\:([^\:]+)(\:([^\:]+))?(\:([^:]+))?/
    def result = [:]
    Matcher m = p.matcher(artifactString)
    if (!m.matches()) {
      throw new IllegalArgumentException("Expected groupdId:artifactId, groupdId:artifactId:type or groupId:artifactId:type:version")
    }
    result['groupId'] = m.group(1)
    result['artifactId'] = m.group(2)
    result['type'] = m.group(4)
    result['version'] = m.group(6)
    return result
  }
}
